package com.jghz.framework.utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**  
*  
* 项目名称：jghz  
* 类名称：DbUtil  
* 类描述：  
* 创建人：Tom  
* 创建时间：2016-11-2 上午11:08:21  
* 修改人：Tom  
* 修改时间：2016-11-2 上午11:08:21  
* 修改备注：  
* @version  
*  
*/
public class DbUtil {
	public static String resetSql(String sql) {
		String ret = "";
		String s = sql;
		s = s.replaceAll("( )(F|f)(R|r)(O|o)(M|m)( )", " FROM ");
		s = s.replaceAll("(.*)(S|s)(E|e)(L|l)(E|e)(C|c)(T|t)( )", " SELECT ");
		s = s.replaceAll("( )(G|g)(R|r)(O|o)(U|u)(P|p)( )(B|b)(Y|y)( )", " GROUP BY ");
		s = s.replaceAll("( )(O|o)(R|r)(D|d)(E|e)(R|r)( )(B|b)(Y|y)( )", " ORDER BY ");
		int indexFrom = s.indexOf("FROM");
		while (s.indexOf("FROM") >= 0) {
			indexFrom = indexFrom + 4;
			String s1 = s.substring(0, indexFrom);
			s = s.substring(indexFrom);
			int indexWhere = s1.indexOf("WHERE");
			if (indexWhere > 0) {
				indexWhere = indexWhere + 5;
				s1 = s1.substring(0, indexWhere) + s1.substring(indexWhere).replace("WHERE", "AND");
			}
			ret += s1;
			indexFrom = s.indexOf("FROM");
		}
		int indexWhere = s.indexOf("WHERE");
		if (indexWhere > 0) {
			indexWhere = indexWhere + 5;
			s = s.substring(0, indexWhere) + s.substring(indexWhere).replace("WHERE", "AND");
		}
		ret += s;
		return ret;
	}

	
	public static String filter(String value) {
        if (value == null) {
            return null;
        }       
        StringBuffer result = new StringBuffer(value.length());
        for (int i = 0; i < value.length(); i++) {
            switch (value.charAt(i)) {
            case '<':
                result.append("&lt;");
                break;
            case '>':
                result.append("&gt;");
                break;
            case '"':
                result.append("&quot;");
                break;
            case '\'':
                result.append("&#39;");
                break;
            case '%':
                result.append("&#37;");
                break;
            case ';':
                result.append("&#59;");
                break;
            case '(':
                result.append("&#40;");
                break;
            case ')':
                result.append("&#41;");
                break;
            case '&':
                result.append("&amp;");
                break;
            case '+':
                result.append("&#43;");
                break;
            default:
                result.append(value.charAt(i));
                break;
            }
        }       
        return result.toString();
	}

	public static List<String> resetParam(List<String> param) {
		if (param == null || param.size() == 0) {
			return null;
		}
		List<String> newParam = new ArrayList<String>(); 
		for (int index = 0; index < param.size(); index++) {
			String value = param.get(index);
			if (value == null || value.equals("")) {
				newParam.add(value);
				continue;
			}
	        String result = filter(value);
	        newParam.add(result);
		}
		return newParam;
	}
	
	public static Object resetObject (Object obj) {
		if (obj == null) {
			return obj;
		}
		String className = obj.getClass().getName();
		try {
			Class<?> objClass = Class.forName(className);
			
			Method[] methods = objClass.getDeclaredMethods();
			for (int i = 0; i < methods.length; i++) {
				Method getMethod = methods[i];
				if (getMethod.getGenericReturnType().equals(String.class)) {
					String value = (String) getMethod.invoke(obj);
					String result = filter(value);
					
					String setName = getMethod.getName().replaceFirst("get", "set");
					Method setMethod = objClass.getDeclaredMethod(setName, String.class);
					Object arglist[] = new Object[1];
					arglist[0] = result;
					setMethod.invoke(obj, arglist);
				}
			}
		} catch (Throwable e) {
			return obj;
		}
		return obj;
	}
	
	/**
	 * 将id串分割为多个字符串数组
	 * <li> 用于in查询 超过1000会报错
	 * @param s id串
	 * @param split 每个分割数组的长度
	 * @return
	 * @author BillGates
	 * @createTime May 23, 20143:20:32 PM
	 */
	public static String[] splitIds(String s,int split){
		String[] ids = s.split(",");
		int yu = ids.length%split;
		int count = yu==0?ids.length/split:ids.length/split+1;
		String[] result = new String[count];
		for(int i=0;i<count;i++){
			StringBuffer sb = new StringBuffer();
			int j = i * split;
			int t = (i+1)*split > ids.length ? ids.length : (i+1)*split;
			for(;j<t;j++){
				if(j != t - 1){
					sb.append(ids[j]+",");
				}else{
					sb.append(ids[j]);
				}
			}
			result[i] = sb.toString();
		}
		return result;
	}

}
